#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>




class PinFuncIndex{
  public:
    PinFuncIndex(int index){this->index=index;}
    ~PinFuncIndex(){}

    int Index(){return index;}
  private:
    int index;
};




MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  connect(ui->tableView_Assignment,static_cast<void (TabviewPinAssignment::*)(void*,int,bool)>(&TabviewPinAssignment::triggered)
          ,this,static_cast<void (MainWindow::*)(void*,int,bool)>(&MainWindow::PinTriggered));
  connect(ui->listView_Functions,static_cast<void (ListviewPinAssignment::*)(void*)>(&ListviewPinAssignment::Clicked)
          ,this,static_cast<void (MainWindow::*)(void*)>(&MainWindow::FunctionClicked));
  
  ui->tableView_Assignment->SetIcon(QIcon(":image/HYC.0x9DEFA478.LOGO_TB.ico"));

  pa=nullptr;

}

MainWindow::~MainWindow(){

  delete ui;
}




void MainWindow::ClearPinFunction(){
  qsizetype count;

  count=pins.count();
  for(qsizetype i=0;i<count;i++){
    delete (PinFuncIndex*)pins[i]->V();
    delete pins[i];
  }

  count=functions.count();
  for(qsizetype i=0;i<count;i++){
    delete (PinFuncIndex*)functions[i]->V();
    delete functions[i];
  }
  
  pins.clear();
  functions.clear();
}




void MainWindow::PinTriggered(void* v,int Index,bool checked){
  PinFuncIndex* pfi;

  Q_UNUSED(checked);

  pfi=(PinFuncIndex*)v;
  if(Index>=0){
    Index-=1;
  }
  pa->FunctionSelect(pfi->Index(),Index);

}

void MainWindow::FunctionClicked(void* v){
  PinFuncIndex* pfi;

  if(pa==nullptr){
    return;
  }

  pfi=(PinFuncIndex*)v;
  pa->HighLightFunction(pfi->Index(),true);

}

void MainWindow::PinFunctionChange(int PinIndex,PinAssignment::PinStatus Status){
  int index;
  //颜色设置

  if(pa==nullptr){
    return;
  }

  TabviewPinAssignment::Pin& pin=pins[PinIndex][0];

  pin.SetBgColor(Status.BgColor());
  index=Status.Index();
  if(index>=0){
    index+=1;
  }
  pin.SetIndex(index);

  ui->tableView_Assignment->SetData(pin,PinIndex);

}

void MainWindow::on_lineEdit_Search_textChanged(const QString& arg1){

  ui->listView_Functions->SetSearchText(arg1);

}

void MainWindow::on_spinBox_GridWidth_valueChanged(int arg1){

  ui->tableView_Assignment->horizontalHeader()->setDefaultSectionSize(arg1);

}

void MainWindow::on_spinBox_GridHeight_valueChanged(int arg1){

  ui->tableView_Assignment->verticalHeader()->setDefaultSectionSize(arg1);
}


void MainWindow::on_actionOpenAssignmentFile_triggered(){
  PinAssignment* pa;

  QString FileName=QFileDialog::getOpenFileName(this,"选择一个引脚存档文件(spa)",QString("C:\\Users\\1001010011000000\\Desktop\\"),"引脚存档(*.spa)");

  if(FileName.isNull()){
    return;
  }

  if(this->pa!=nullptr){
    ClearPinFunction();
    delete this->pa;
  }

  pa=new PinAssignment();

  qint64 lastT;
  qint64 dt;

  lastT=QDateTime::currentDateTime().toMSecsSinceEpoch();

  dt=QDateTime::currentDateTime().toMSecsSinceEpoch()-lastT;
  lastT+=dt;
  qDebug()<< "Start:" << dt <<Qt::endl;

  try{
    pa->LoadFile(FileName);
  }catch(const QString& e){
    QMessageBox::information(this,"错误",QString("打开文件失败：")+e);
    delete pa;
    return;
  }catch(...){
    QMessageBox::information(this,"错误","打开文件失败：未知错误");
    delete pa;
    return;
  }
  this->pa=pa;

  dt=QDateTime::currentDateTime().toMSecsSinceEpoch()-lastT;
  lastT+=dt;
  qDebug()<< "LoadFileDone:" << dt <<Qt::endl;
  
  QStringList FunctionList;
  QList<PinAssignment::PinInfo> PinList;

  pa->GetPinList(PinList);
  pa->GetFunctionList(FunctionList);
  
  for(qsizetype i=0,count=PinList.count();i<count;i++){
    QStringList list=PinList[i].Functions();
    list.insert(0,"Reset");
    TabviewPinAssignment::Pin* pin=new TabviewPinAssignment::Pin(PinList[i].Name(),new PinFuncIndex(PinList[i].Index()),list,QBrush(QColor(0x00,0x00,0x00)));
    pins<<pin;
  }

  for(qsizetype i=0,count=FunctionList.count();i<count;i++){
    ListviewPinAssignment::Function* fun=new ListviewPinAssignment::Function(FunctionList[i],new PinFuncIndex(i),QBrush(QColor(0xFF,0xFF,0xFF)));
    functions<<fun;
  }

  connect(pa,&PinAssignment::PinFunctionChange,this,&MainWindow::PinFunctionChange);
  
  int W;
  int H;
  pa->GetPackageSize(&W,&H);
  ui->tableView_Assignment->SetSize(H,W);
  ui->tableView_Assignment->SetData(pins);
  ui->listView_Functions->SetData(functions);

  pa->AllPinUpdate();

  ui->actionSaveAssignmentFile->setEnabled(true);

}


void MainWindow::on_actionNewAssignmentFile_triggered(){
  PinAssignment* pa;

  QString FileName=QFileDialog::getOpenFileName(this,"选择一个引脚配置文件(hpa)",QString("C:\\Users\\1001010011000000\\Desktop\\"),"引脚配置(*.hpa)");

  if(FileName.isNull()){
    return;
  }

  if(this->pa!=nullptr){
    ClearPinFunction();
    delete this->pa;
  }

  pa=new PinAssignment();

  connect(pa,&PinAssignment::PinFunctionChange,this,&MainWindow::PinFunctionChange);

  qint64 lastT;
  qint64 dt;

  lastT=QDateTime::currentDateTime().toMSecsSinceEpoch();

  dt=QDateTime::currentDateTime().toMSecsSinceEpoch()-lastT;
  lastT+=dt;
  qDebug()<< "Start:" << dt <<Qt::endl;

  try{
    pa->LoadFile(FileName);
  }catch(const QString& e){
    QMessageBox::information(this,"错误",QString("打开文件失败：")+e);
    delete pa;
    return;
  }catch(...){
    QMessageBox::information(this,"错误","打开文件失败：未知错误");
    delete pa;
    return;
  }
  this->pa=pa;

  dt=QDateTime::currentDateTime().toMSecsSinceEpoch()-lastT;
  lastT+=dt;
  qDebug()<< "LoadFileDone:" << dt <<Qt::endl;
  
  QStringList FunctionList;
  QList<PinAssignment::PinInfo> PinList;

  pa->GetPinList(PinList);
  pa->GetFunctionList(FunctionList);
  
  for(qsizetype i=0,count=PinList.count();i<count;i++){
    QStringList list=PinList[i].Functions();
    list.insert(0,"Reset");
    TabviewPinAssignment::Pin* pin=new TabviewPinAssignment::Pin(PinList[i].Name(),new PinFuncIndex(PinList[i].Index()),list,QBrush(QColor(0x00,0x00,0x00)));
    pins<<pin;
  }

  for(qsizetype i=0,count=FunctionList.count();i<count;i++){
    ListviewPinAssignment::Function* fun=new ListviewPinAssignment::Function(FunctionList[i],new PinFuncIndex(i),QBrush(QColor(0xFF,0xFF,0xFF)));
    functions<<fun;
  }
  
  int W;
  int H;
  pa->GetPackageSize(&W,&H);
  ui->tableView_Assignment->SetSize(H,W);
  ui->tableView_Assignment->SetData(pins);
  ui->listView_Functions->SetData(functions);

  pa->AllPinUpdate();

  ui->actionSaveAssignmentFile->setEnabled(true);


}


void MainWindow::on_actionSaveAssignmentFile_triggered(){
  QString FileName=QFileDialog::getSaveFileName(this,"选择保存路径",QString("C:\\Users\\1001010011000000\\Desktop\\"),"引脚存档(*.spa)");

  if(FileName.isNull()){
    return;
  }

  pa->SaveFile(FileName);
}

